package com.beney.app.context;

import com.beney.common.SystemIn;
import com.beney.service.UserServiceImpl;
import com.beney.util.Commands;

/**
 * 登录状态
 *
 * @author Beney
 */
public class LoginState extends AbstractState {
    protected LoginState(AppContext context) {
        super(context);
        super.setUserService(new UserServiceImpl());
    }

    @Override
    void runState()
    {
        System.out.print(Commands.wrapHintInputCommand(
                "", // 初始没有用户
                "",
                "请输入用户名和密码，两者之间用空格隔开"));

        String name, pwd;

        name = SystemIn.CIN.next();
        pwd = SystemIn.CIN.nextLine().trim();
        if (name != null && pwd != null
                && super.userService.initService(name, pwd))
        {
            System.out.print(Commands.wrapHintCommand(
                    "当前选择为",
                    userService.getIdentity(),
                    "出题"
            ));
            context.shiftState(context.listeningState, this.userService);
        }
        else System.out.println(Commands.wrapHintCommand(
                "请输入正确的用户名、密码"
        ));
    }
}
